package com.ptswitch.material.service.impl;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hxzon.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ptswitch.material.dao.ContractMapper;
import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.service.ContractService;

@Service
public class ContractServiceImpl implements ContractService {

    private static final Logger logger = LoggerFactory.getLogger(ContractServiceImpl.class);
    @Autowired
    private ContractMapper contractMapper;

    @Override
    public Contract findById(long id, int init) {
        Contract contract = contractMapper.findById(id, init);
        return contract;
    }

    @Override
    public List<Contract> findAllByContractStatus(String status, long first, long size) {
        return contractMapper.findAllByContractStatus(status, first, size);
    }

    @Override
    public List<Contract> findByVisitor(Uzer visitor, long first, long size) {
//        String hql = " where p.status='" + Contract.Status_UnOk + "'";
//        switch (visitor.getDepartment()) {
//        case Uzer.Department_Buyer:
//            hql += " and p.creater.id=:userId";
//            break;
//        case Uzer.Department_Financial:
//            hql += " and (p.numUnreplyPayment >0 or p.numUnreplyInvoice >0 ) and p.financial.id=:userId";
//            break;
//        case Uzer.Department_Production:
//            hql += " and (p.numUnpassAndUnexamine >0) and p.production.id=:userId";
//            break;
//        case Uzer.Department_ContractAuditor:
//            //FIXME
//            hql = " where (p.status='" + Contract.Status_UnOk + "' or p.askDelete=true or p.askReset=true) ";
//            hql += " and p.auditor.id=:userId";
//            break;
//        }
        return null;
    }

    @Override
    public void save(Contract model) {
//		model.setCreateTime(DateUtil.formatToUniteTime(new Date()));
        contractMapper.save(model);
    }

    private Pattern p = Pattern.compile(".*-\\d*-(\\d)");

    @Override
    public String emptyCode(String code) {
        code = StringUtil.escapeSql(code);
        String lastCode = contractMapper.emptyCode(code);
        logger.debug("get empty code:{}", lastCode);
        Matcher m = p.matcher(lastCode);
        try {
            m.matches();
            int index = Integer.parseInt(m.group(1)) + 1;
            return code + "-" + index;
        } catch (RuntimeException e) {
            logger.error("get empty code fail", e);
        }
        return code;
    }

    @Override
    public Contract findById(long id) {
        return null;
    }

    @Override
    public void delete(Contract contract) {

    }

    @Override
    public List<Contract> findAll(long first, long size) {
        return null;
    }

    @Override
    public long countAll() {
        return 0;
    }

    @Override
    public List<Contract> findLike(String like, long first, long size) {
        return null;
    }

    @Override
    public long countLike(String like) {
        return 0;
    }

}
